package com.ilibrary.web.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import com.ilibrary.base.MyAjaxAction;
import com.ilibrary.base.MyBaseVO;
import com.ilibrary.constants.MyConstants;
import com.ilibrary.constants.MyMsgConstants;
import com.ilibrary.service.UserInfoService;
import com.ilibrary.web.dto.ResultDto;
import com.ilibrary.web.vo.UserInfoVO;

public class AjaxDoUpdateUserPasswordAction extends MyAjaxAction {

	private UserInfoService userInfoService;
	
	public UserInfoService getUserInfoService() {
		return userInfoService;
	}
	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}

	@Override
	protected JSONObject doExecute(HttpServletRequest request,
			HttpServletResponse response, HttpSession session) throws Exception {
		JSONObject jo = new JSONObject();
		UserInfoVO userInfoVO = (UserInfoVO) session.getAttribute(MyConstants.ILIBRARY_lOGIN_USER);
		String oldPassword = request.getParameter("oldPassword");
		String newPassword = request.getParameter("newPassword");
		if(userInfoVO == null) {
			jo.put("success", false);
			jo.put("error", MyMsgConstants.SYSTEM_ERROR);
			return jo;
		}else {
			ResultDto<MyBaseVO> resultDto = userInfoService.updateUserPassword(userInfoVO.getUserId(), oldPassword, newPassword);
			if(!resultDto.isSuccess()) {
				jo.put("success", false);
				jo.put("error", resultDto.getMsg());
			}else {
				jo.put("success", true);
			}
			return jo;
		}
	}

}
